package keeto.com.vn.bms.ui.activity;

import java.util.ArrayList;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.ui.widget.CallDialog;
import keeto.com.vn.bms.ui.widget.LogoutDialog;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import keeto.com.vn.bms.utils.Utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SupportActivity extends Activity implements OnClickListener {
	private ArrayList<Drawable> icons = new ArrayList<Drawable>();
	private ArrayList<String> names = new ArrayList<String>();

	private ImageButton btnBack;
	private TextView tvTitle;
	private ImageButton btnLogin;

	private RelativeLayout rl_support_leadership;
	private ImageView img_icon_support_leadership;
	private ImageView imv_support_arraw_leadership;
	private LinearLayout ll_support_leadership;
	private EditText edt_support_title_leadership;
	private EditText edt_support_content_leadership;
	private Button btn_send_support_leadership;

	private RelativeLayout rl_support_admin;
	private ImageView img_icon_support_admin;
	private ImageView imv_support_arraw_admin;
	private LinearLayout ll_support_admin;
	private EditText edt_support_title_admin;
	private EditText edt_support_content_admin;
	private Button btn_send_support_admin;
	private TextView tvContentLeader;
	private TextView tvContentAdmin;

	private LinearLayout ll_support_call;

	private BMSSharedPreferences preferences;
	private static Context context;

	public static String sPhone = "com.android.contacts";

	public static String sKakao = "com.kakao.talk";
	public static String sSkype = "com.skype.raider";
	public static String sViber = "com.viber.voip";

	public static void actionLaunch(Context ctx) {
		Intent i = new Intent(ctx, SupportActivity.class);
		ctx.startActivity(i);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.support_activity_layout);
		preferences = new BMSSharedPreferences(this);
		initUIComponenet();

	}

	private void initUIComponenet() {
		btnBack = (ImageButton) findViewById(R.id.btn_tittle_back);
		tvTitle = (TextView) findViewById(R.id.tv_title_name);
		btnLogin = (ImageButton) findViewById(R.id.btn_tittle_login);
		tvContentAdmin=(TextView)findViewById(R.id.tv_content_admin);
		tvContentLeader=(TextView)findViewById(R.id.tv_content_leader);
		tvContentAdmin.setText(Html.fromHtml(getString(R.string.detail_hospital) + " "
				+ "<font color=" + getResources().getColor(R.color.orange)
				+ ">*</font>"));
		tvContentLeader.setText(Html.fromHtml(getString(R.string.detail_hospital) + " "
				+ "<font color=" + getResources().getColor(R.color.orange)
				+ ">*</font>"));
		rl_support_leadership = (RelativeLayout) findViewById(R.id.rl_support_leadership);
		img_icon_support_leadership = (ImageView) findViewById(R.id.img_icon_support_leadership);
		imv_support_arraw_leadership = (ImageView) findViewById(R.id.imv_support_arraw_leadership);
		ll_support_leadership = (LinearLayout) findViewById(R.id.ll_support_leadership);
		edt_support_title_leadership = (EditText) findViewById(R.id.edt_support_title_leadership);
		edt_support_content_leadership = (EditText) findViewById(R.id.edt_support_content_leadership);
		btn_send_support_leadership = (Button) findViewById(R.id.btn_send_support_leadership);

		rl_support_admin = (RelativeLayout) findViewById(R.id.rl_support_admin);
		img_icon_support_admin = (ImageView) findViewById(R.id.img_icon_support_admin);
		imv_support_arraw_admin = (ImageView) findViewById(R.id.imv_support_arraw_admin);
		ll_support_admin = (LinearLayout) findViewById(R.id.ll_support_admin);
		edt_support_title_admin = (EditText) findViewById(R.id.edt_support_title_admin);
		edt_support_content_admin = (EditText) findViewById(R.id.edt_support_content_admin);
		btn_send_support_admin = (Button) findViewById(R.id.btn_send_support_admin);

		ll_support_call = (LinearLayout) findViewById(R.id.ll_support_call);

		ll_support_leadership.setVisibility(View.GONE);
		ll_support_admin.setVisibility(View.GONE);

		btnBack.setOnClickListener(this);
		btnLogin.setOnClickListener(this);
		tvTitle.setText(this.getString(R.string.category_support));
		rl_support_leadership.setOnClickListener(this);
		rl_support_admin.setOnClickListener(this);
		btn_send_support_leadership.setOnClickListener(this);
		btn_send_support_admin.setOnClickListener(this);
		ll_support_call.setOnClickListener(this);

		btnLogin.setBackgroundResource(Utils.getLogobyPermission(preferences
				.getUserPermission(SupportActivity.this)));

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_tittle_back:
			this.finish();

			break;
		case R.id.btn_tittle_login:
			if (preferences.getUserAuthKey(SupportActivity.this).equals("")){
				LoginBMSActivity.actionLaunch(SupportActivity.this);
			}else{
				LogoutDialog dialog = new LogoutDialog(SupportActivity.this);
				dialog.show();
			}
			break;
		case R.id.rl_support_leadership:
			ll_support_leadership.setVisibility(View.VISIBLE);
			ll_support_admin.setVisibility(View.GONE);
			img_icon_support_leadership.setSelected(true);
			imv_support_arraw_leadership.setSelected(true);
			img_icon_support_admin.setSelected(false);
			imv_support_arraw_admin.setSelected(false);

			break;
		case R.id.rl_support_admin:
			ll_support_leadership.setVisibility(View.GONE);
			ll_support_admin.setVisibility(View.VISIBLE);
			img_icon_support_leadership.setSelected(false);
			imv_support_arraw_leadership.setSelected(false);
			img_icon_support_admin.setSelected(true);
			imv_support_arraw_admin.setSelected(true);

			break;
		case R.id.btn_send_support_leadership:
			String groupID = "5";
			sendToAdmin(groupID, edt_support_title_leadership,
					edt_support_content_leadership);
			break;
		case R.id.btn_send_support_admin:
			String title = edt_support_title_admin.getText().toString();
			String content = edt_support_content_admin.getText().toString();
			String group_Id = "4";
			sendToAdmin(group_Id, edt_support_title_admin,
					edt_support_content_admin);
			break;
		case R.id.ll_support_call:

			icons.clear();
			getIcons();
			if (icons != null) {
				CallDialog dialog = new CallDialog(this, icons, names);
				dialog.show();
			}
			break;

		default:
			break;
		}

	}

	public void sendToAdmin(String groupID, EditText edtTitle,
			EditText edtContent) {
		// String content = edtContent.getText().toString();
		if ((!edtTitle.getText().toString().equals("")
				&& !edtContent.getText().toString().equals(""))||(edtTitle.getText().toString().equals("")
						&& !edtContent.getText().toString().equals(""))) {
			BMSCore.getInstance(this).taskSendToAdmin(
					edtContent.getText().toString(),
					edtTitle.getText().toString(), groupID,
					preferences.getUserAuthKey(context), mListenner);
			String log = preferences.getUserAuthKey(context).toString();
			Log.e("Log", log);
			edtContent.setText("");
			edtTitle.setText("");
			Toast.makeText(SupportActivity.this, getString(R.string.send_ok),
					Toast.LENGTH_SHORT).show();
		} else if(edtTitle.getText().toString().equals("")
				&& edtContent.getText().toString().equals("")) {
			Toast.makeText(SupportActivity.this, getString(R.string.warning),
					Toast.LENGTH_SHORT).show();
		}else if(!edtTitle.getText().toString().equals("")
				&& edtContent.getText().toString().equals("")) {
			Toast.makeText(SupportActivity.this, getString(R.string.warning_content),
					Toast.LENGTH_SHORT).show();
		}
	}

	private Listenner mListenner = new Listenner();

	public void handleLoginLogout(final Context ctx, final BMSListener listener) {
		// TODO Auto-generated method stub
		if (preferences.getUserAuthKey(ctx).equals("")
				|| preferences.getUserAuthKey(ctx).equals(null)) {
			LoginBMSActivity.actionLaunch(ctx);
		} else {
			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
					ctx);
			alertDialogBuilder.setTitle(ctx.getResources().getString(
					R.string.btn_primary_logout)
					+ "");
			alertDialogBuilder
					.setMessage(
							ctx.getResources().getString(R.string.message_log)
									+ "!")
					.setCancelable(false)
					.setPositiveButton(
							ctx.getResources().getString(R.string.yes),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									BMSCore.getInstance(ctx).taskLogout(
											preferences.getUserAuthKey(ctx),
											listener);
								}
							})
					.setNegativeButton(
							ctx.getResources().getString(R.string.canel_log),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			AlertDialog alertDialog = alertDialogBuilder.create();
			alertDialog.show();
		}

	}

	class Listenner extends BMSListener {

		@Override
		public void finishSendToAdmin(final int returnCode, String resultMsg) {
			// TODO Auto-generated method stub
			super.finishSendToAdmin(returnCode, resultMsg);
			runOnUiThread(new Runnable() {
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK) {
						
						Toast.makeText(SupportActivity.this, SupportActivity.this.getResources().getString(R.string.sussess),
								Toast.LENGTH_SHORT).show();

					}
					else if(returnCode==IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(SupportActivity.this, SupportActivity.this.getResources().getString(R.string.send_not_ok),
								Toast.LENGTH_SHORT).show();
					} 
					else{
						Toast.makeText(SupportActivity.this, SupportActivity.this.getResources().getString(R.string.send_not_ok),
								Toast.LENGTH_SHORT).show();
					}
				}
			});

		}

		protected void showMessageToast(final String message) {
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(SupportActivity.this, message,
							Toast.LENGTH_SHORT).show();
				}
			});
		}

		@Override
		public void finishLogout(final int returnCode, final String msg) {
			// TODO Auto-generated method stub
			super.finishLogout(returnCode, msg);
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK) {
						preferences.setUserAuthKey("");
						preferences.setUserPermission(0);
						HomeBMSActivity.actionLaunch(SupportActivity.this);
						SupportActivity.this.finish();
						Toast.makeText(SupportActivity.this, SupportActivity.this.getResources().getString(R.string.sussess),
								Toast.LENGTH_SHORT).show();

					}
					else if(returnCode==IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(SupportActivity.this, SupportActivity.this.getResources().getString(R.string.error_data_server),
								Toast.LENGTH_SHORT).show();
					} 
					else{
						Toast.makeText(SupportActivity.this, SupportActivity.this.getResources().getString(R.string.fail),
								Toast.LENGTH_SHORT).show();
					}
				}
			});

		}
	}

	public ArrayList<Drawable> getIcons() {

		try {
			Drawable iCallPhone;
			iCallPhone = getBaseContext().getPackageManager()
					.getApplicationIcon(sPhone);
			icons.add(iCallPhone);
			names.add("Phone");
		} catch (PackageManager.NameNotFoundException ne) {

		}
		try {
			Drawable iKakao;
			iKakao = getBaseContext().getPackageManager().getApplicationIcon(
					sKakao);
			icons.add(iKakao);
			names.add("KakaoTalk");
		} catch (PackageManager.NameNotFoundException ne) {

		}

		try {
			Drawable iSkype;
			iSkype = getBaseContext().getPackageManager().getApplicationIcon(
					sSkype);
			icons.add(iSkype);
			names.add("Skype");
			// bSkype = (ImageButton) findViewById(R.id.bSkype);
			// bSkype.setImageDrawable(iSkype);
		} catch (PackageManager.NameNotFoundException ne) {

		}

		try {
			Drawable iViber;
			iViber = getBaseContext().getPackageManager().getApplicationIcon(
					sViber);
			icons.add(iViber);
			names.add("Viber");
			// bViber = (ImageButton) findViewById(R.id.bViber);
			// bViber.setImageDrawable(iViber);
		} catch (PackageManager.NameNotFoundException ne) {

		}
		return icons;

	}

}
